$(document).ready(function() {
		onStart();
		loadUpcomingCases();
		
	
		$("#addCase").click(function() {
			if( document.getElementById("plaintiff").value === '' ){
				alert("Plaintiff cannot be empty");
				return;
			}
			if( document.getElementById("respondent").value === ''){
				alert("Respondent cannot be empty");
				return;
			}
			var caseObj = getDummyCaseObject();
			
			caseObj.caseRefNo = document.getElementById("caseRefNo").value;
			caseObj.plaintiff = document.getElementById("plaintiff").value;
			caseObj.respondent = document.getElementById("respondent").value;
			caseObj.caseNote = document.getElementById("caseNote").value;
			addCase(caseObj);
			loadAllCases();
		});
		$("#updateCase").click(function() {
			$.mobile.changePage("#updateCasePage");
		});
		$("#showCase").click(function() {
			getAllCases();
		});
		$("#addDate").click(function() {
			if($("#date").val() === ''){
				alert("Date cannot be empty");
				return;
			}
			if($("#dateNote").val() === ''){
				alert("Date Notes cannot be empty");
				return;
			}
			var caseDate = document.getElementById("date").value;
			var dateNote = document.getElementById("dateNote").value;
			var caseId = document.getElementById("caseId").value;
			addDate(caseId, caseDate, dateNote)
			getCaseData(caseId);
		});
		$("#activeCases").click(function() {
			loadAllCases();
		});
		$("#addDatePanel").click(function() {
			chooseCase();
		});
		$("#addCasePanel").click(function() {
			$.mobile.changePage("#addCasePage", { transition: "slide" });
		});
		$("#upcomingCases").click(function() {
			loadUpcomingCases();
			$.mobile.changePage("#upcomingCasesPage", { transition: "slide" });
		});
		$("#addCaseFooter").click(function() {
			$.mobile.changePage("#addCasePage", { transition: "slide" });
		});
		$("#searchButton").click(function() {
			loadSearchPage()
			
		});
		
	});
	
	function loadUpcomingCases(){
	
	    
		var result = upcomingCases();

		$("#upcomingCasesPage").empty();
		
		$("#upcomingCasesPage").append('<div data-role="header" data-position="fixed"><a href="#mypanel" class="ui-btn ui-corner-all ui-icon-bars ui-btn-icon-notext"></a><h1>Upcoming Dates</h1><a href="#" class="ui-btn ui-corner-all ui-icon-search ui-btn-icon-notext" id="searchButton">Search Icon</a></div><div data-role="main" class="ui-content"><ul id="upcomingCasesList" data-role="listview" data-divider-theme="a" data-inset="true" data-icon="false"></ul></div>');
		
		var tempDate = new Date();
		var today = new Date(tempDate.getFullYear(),tempDate.getMonth(),tempDate.getDate())
		var tomorrow = new Date(today.getFullYear(),today.getMonth(),today.getDate()+1)
		var dateString;
		for(var i=result.length-1;i>=0;i-- ){
		var date = new Date(result[i].date.getFullYear(),result[i].date.getMonth(),result[i].date.getDate())
		if(date.getTime() == today.getTime() ){
			dateString = "Today";
		}
		else if(date.getTime() == tomorrow.getTime()){
			dateString = "Tomorrow";
		}
		else{
			dateString = result[i].date.toDateString();
		}
		$("#upcomingCasesList").append('<li data-role="list-divider">'+dateString+'</li>'); 
		$("#upcomingCasesList").append('<li><a onclick="loadUpdateDate('+result[i].caseId+','+result[i].dateId+')">'+result[i].plaintiff+' vs '+result[i].respondent+'</a></li>'); 
		}
		
		 $("#upcomingCasesPage").append('</div><div data-role="footer" data-position="fixed" style="text-align:center;"><a href="#" class="ui-btn ui-btn-b ui-corner-all ui-icon-plus ui-btn-icon-top" id="addCaseFooter">Add Case</a></div>');
		
			
		$('#bodyId').enhanceWithin();
			
		//$.mobile.changePage("#upcomingCasesPage", { transition: "slide" });	
	}
	
	function loadAllCases() {
	
			var activeCases =JSON.parse(localStorage.getItem("activeCases"));
			$("#allCasesPage").empty();
			 $("#allCasesPage").append('<div data-role="header" data-position="fixed"><a href="#mypanel" class="ui-btn ui-corner-all ui-icon-bars ui-btn-icon-notext"></a><h1> All Cases</h1><a href="#" class="ui-btn ui-corner-all ui-icon-search ui-btn-icon-notext" id="searchButton">Search Icon</a></div><div data-role="main" class="ui-content"><ul id="allCasesList" data-role="listview" data-inset="true" data-icon="false"></ul></div>')
			for(var i=0;i<activeCases.length;i++ ){
			  var cases = JSON.parse(localStorage.getItem(activeCases[i]));
			  $("#allCasesList").append('<li><a onclick="getCaseData('+activeCases[i]+')">'+cases.plaintiff+' vs '+cases.respondent+'</a></li>'); 
			}
			
			$('#bodyId').enhanceWithin();
			
			$.mobile.changePage("#allCasesPage", { transition: "slide" });	
			
	}
	
	function chooseCase() {
	
			var activeCases =JSON.parse(localStorage.getItem("activeCases"));
			$("#chooseCasePage").empty();
			 $("#chooseCasePage").append('<div data-role="header" data-position="fixed"><a href="#mypanel" class="ui-btn ui-corner-all ui-icon-bars ui-btn-icon-notext"></a><h1>Choose Case</h1><a href="#" class="ui-btn ui-corner-all ui-icon-search ui-btn-icon-notext" id="searchButton">Search Icon</a></div><div data-role="main" class="ui-content"><ul id="chooseCases" data-role="listview" data-inset="true" data-icon="false"></ul></div>')
			for(var i=0;i<activeCases.length;i++ ){
			  var cases = JSON.parse(localStorage.getItem(activeCases[i]));
			  $("#chooseCases").append('<li><a onclick="addDateToPage('+activeCases[i]+')">'+cases.plaintiff+' vs '+cases.respondent+'</a></li>'); 
			}
			
			$('#bodyId').enhanceWithin();
			
			$.mobile.changePage("#chooseCasePage", { transition: "slide" });
			
	}
				
	function getCaseData(caseId){
		var caseObj = getCase(caseId);
		var dates = caseObj.dates;
	    $("#viewCasePage").empty();
		$("#viewCasePage").append('<div data-role="header" data-position="fixed"><a href="#mypanel" class="ui-btn ui-corner-all ui-icon-bars ui-btn-icon-notext"></a><h1> Add Case</h1><a href="#" class="ui-btn ui-corner-all ui-icon-search ui-btn-icon-notext"  id="searchButton">Search Icon</a></div><div data-role="main" id="viewCaseData" class="ui-content">');		
		$("#viewCaseData").append("Case Ref No : "+caseObj.caseRefNo);
		$("#viewCaseData").append("</br>Plaintiff   : "+caseObj.plaintiff);
		$("#viewCaseData").append("</br>Respondent   : "+caseObj.respondent);
		$("#viewCaseData").append("</br>Case Note   : "+caseObj.caseNote);
		$("#viewCaseData").append('<ul id="allDates" data-role="listview" data-inset="true"  data-icon="false"></ul>');
 
		for (var counter = 0; counter < dates.length; counter++) {
				$("#allDates").append('<li><a onclick="loadUpdateDate('+caseId+','+dates[counter].dateId+')">'+(new Date(dates[counter].caseDate)).toDateString()+' </br> '+dates[counter].dateNote+'</a></li>'); 
		}
	

	    $("#viewCasePage").append('</div><div data-role="footer" data-position="fixed" style="text-align:center;"><a id="addDateButton" data-icon="plus" data-theme="b" onclick="addDateToPage('+caseId+')">Add Date</a><a id="loadUpdateCasePageButton" data-icon="edit" data-theme="b" onclick="loadUpdateCasePage('+caseId+')">Edit</a><a id="deleteCaseButton" data-theme="b" data-icon="delete" onclick="deleteCaseImpl('+caseId+')">Delete</a>');
		
		$('#bodyId').enhanceWithin();
		$.mobile.changePage("#viewCasePage",{ transition: "slide" });
		
	
	}
	function addDateToPage(caseId){
	$("#addDatePage").append("<input type='hidden' id='caseId' value='"+caseId+"' >");
	$.mobile.changePage("#addDatePage", { transition: "slide" });
	}
	function deleteCaseImpl(caseId){
		var option = confirm("Do you want to delete the case ?")
	
		if(option == true){
			deleteCase(caseId);
			loadAllCases();
		}
	}
	function loadUpdateCasePage(caseId){
	
		
		var caseObj = getCase(caseId);
		$("#caseRefNoUpdate").val(caseObj.caseRefNo);
		$("#plaintiffUpdate").val(caseObj.plaintiff);
		$("#respondentUpdate").val(caseObj.respondent);
		$("#caseNoteUpdate").val(caseObj.caseNote);
		$("#updateCaseButton").attr("onclick","updateCaseImpl("+caseId+")");
		$.mobile.changePage("#updateCasePage", { transition: "slide" });
	
	
	}
	function updateCaseImpl(caseId){
	
			var caseObj = getDummyCaseObject();
			if( document.getElementById("plaintiffUpdate").value === '' ){
				alert("Plaintiff cannot be empty");
				return;
			}
			if( document.getElementById("respondentUpdate").value === ''){
				alert("Respondent cannot be empty");
				return;
			}
			
			caseObj.caseRefNo = document.getElementById("caseRefNoUpdate").value;
			caseObj.plaintiff = document.getElementById("plaintiffUpdate").value;
			caseObj.respondent = document.getElementById("respondentUpdate").value;
			caseObj.caseNote = document.getElementById("caseNoteUpdate").value;
			
			updateCase(caseId,caseObj);
			loadAllCases();
	}
	function getDateData(caseId,dateId){
		
		var dateObj = getDate(caseId,dateId)
		$("#viewDateData").html("");
		$("#viewDateData").append("<table><tr><td>Date</td><td>:"+(new Date(dateObj.caseDate)).toDateString()+"</td></tr>");
		$("#viewDateData").append("<tr><td>Date Notes</td><td>:"+dateObj.dateNote+"</td></tr></table>");
		$("#loadUpdateDatePage").attr("onclick","loadUpdateDate("+caseId+","+dateId+")");
		$("#deleteDateButton").attr("onclick","deleteDateImpl("+caseId+","+dateId+")");
		$.mobile.changePage("#datePage", { transition: "slide" });
	
	}
	function loadUpdateDate(caseId,dateId){
		var dateObj = getDate(caseId,dateId);
		$("#updateDate").val(dateObj.caseDate);
		$("#updateDateNote").val(dateObj.dateNote);
		$("#updateDateButton").attr("onclick","updateDateImpl("+caseId+","+dateId+")");
		$("#deleteDateButton").attr("onclick","deleteDateImpl("+caseId+","+dateId+")");
		$.mobile.changePage("#updateDatePage", { transition: "slide" });
		
	}
	function deleteDateImpl(caseId,dateId){
	  var option = confirm("Do you want to delete the date ?")
	
		if(option == true){
			deleteDate(caseId,dateId);
			getDateData(caseId,dateId);
		}
	}
	function updateDateImpl(caseId,dateId){
		if($("#updateDate").val() === ''){
			alert("Date cannot be empty");
			return;
		}
		if($("#updateDateNote").val() === ''){
			alert("Date Notes cannot be empty");
			return;
		}
		var caseDate = $("#updateDate").val();
		var dateNote = $("#updateDateNote").val();
		updateDate(caseId,dateId,caseDate,dateNote);
		getCaseData(caseId);
	}
	function loadSearchPage(){
		var activeCases =JSON.parse(localStorage.getItem("activeCases"));
			$("#searchPage").empty();
			 $("#searchPage").append('<div data-role="header" data-position="fixed"><a href="#mypanel" class="ui-btn ui-corner-all ui-icon-bars ui-btn-icon-notext"></a><h1>Search</h1></div><div data-role="main" class="ui-content"><ul id="searchList" data-role="listview" data-inset="true" data-filter="true" data-filter-reveal="true" data-filter-placeholder="Search..." data-icon="false"></ul></div>');
			for(var i=0;i<activeCases.length;i++ ){
			  var cases = JSON.parse(localStorage.getItem(activeCases[i]));
			  $("#searchList").append('<li><a onclick="getCaseData('+activeCases[i]+')">'+cases.plaintiff+' vs '+cases.respondent+'</a></li>'); 
			}
			
			$('#bodyId').enhanceWithin();
			
			$.mobile.changePage("#searchPage", { transition: "flow" });
	
	
	
	}
	

	
	